package com.msl.base;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.fragment.app.Fragment;
import com.google.gson.Gson;
import com.msl.R;
import com.msl.broadcast.NetWorkChangReceiver;
import com.msl.dialog.WaitDialog;
import com.msl.scan.BroadcastConfig;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;


//基础Fragment
public  class BaseFragment extends Fragment {

	private String tag ="BaseFragment";
	protected View mView = null;
	//app配置信息
	protected Gson gson = null;
	protected WaitDialog waitDialog;
	protected OkHttpClient client;
	protected String result="";
	protected NetWorkChangReceiver netWorkChangReceiver;

	protected void initBroadcastReciever() {
		// 发送广播到扫描工具内的应用设置项
		Intent intent = new Intent(BroadcastConfig.BROADCAST_SETTING);
		// 修改扫描工具内应用设置中的开发者项下的广播名称
		intent.putExtra(BroadcastConfig.BROADCAST_KEY, BroadcastConfig.CUSTOM_NAME);

//		intent.putExtra(BroadcastConfig.SEND_KEY, "BROADCAST");
//		intent.putExtra(BroadcastConfig.END_KEY, "NONE");

		intent.putExtra(BroadcastConfig.VIBERATE_KEY, false);

		intent.putExtra(BroadcastConfig.SOUND_KEY, true);

		intent.putExtra(BroadcastConfig.CONTINIU_KEY, false);

//		intent.putExtra("interval", 1000);

		getActivity().sendBroadcast(intent);
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  // 设置activity显示只能竖屏
		gson = new Gson();
		waitDialog = new WaitDialog(getContext() , R.style.Dialog1);
		WindowManager.LayoutParams lp = waitDialog.getWindow().getAttributes();
		lp.gravity = Gravity.CENTER;
//		client = new OkHttpClient();
		client = new OkHttpClient.Builder()
				.retryOnConnectionFailure(true)
				.connectTimeout(90, TimeUnit.SECONDS)//设置连接超时时间
				.readTimeout(90, TimeUnit.SECONDS)//设置读取超时时间
				.build();

		netWorkChangReceiver = new NetWorkChangReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		getActivity().registerReceiver(netWorkChangReceiver, filter);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		getActivity().unregisterReceiver(netWorkChangReceiver);
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
	}
}
